<?php

require_once 'XML/RPC.php';

class XML_RPC_Fault {
    var $code;
    var $message;
    function __construct( $code, $message ) {
        $this->code = $code;
        $this->message = $message;
    }
}

class XML_RPC_QuickClient {
    var $error = false;
    var $driver = null;
    var $if = null;
    var $prefix = null;
    
    function XML_RPC_QuickClient( $params ) {
        // default
        if( !array_key_exists( 'driver', $params ) ) {
            $params['driver'] = 'pear';
        }
        $this->driver = $params['driver'];
        $this->prefix = (string) $params['prefix'];
        
        if( $params['driver'] == 'pear' ) {
            unset( $params['driver'] );
            require_once 'XML/RPC.php';
            
            $this->if = new XML_RPC_Client( $params['path'], $params['host'],
                                            (int) $params['port'], $params['proxy'],
                                            $params['proxy_port'], $params['proxy_user'],
                                            $params['proxy_pass'] );
        } else {
            $this->error = 'Invalid driver';
        }
    }
    
    function __call( $func, $params ) {
        if( $this->driver == 'pear' ) {
            if( !$params['no_prefix'] ) {
                $func = $this->prefix . $func;
                unset( $params['do_prefix'] );
            }
            $params = array_map( 'XML_RPC_encode', $params );
            $msg = new XML_RPC_Message( $func, $params );
            $resp = $this->if->send( $msg );
            if( !$resp ) {
                return new XML_RPC_Fault( -2, "Transmission error" );
            } else if( $resp->faultCode() ) {
                return new XML_RPC_Fault( $resp->faultCode(), $resp->faultString() );
            } else {
                return XML_RPC_decode( $resp->value() );
            }
        } else {
            return new XML_RPC_Fault( -1, "Invalid driver" );
        }
    }
}


function getAvatar($userinfo) {
	// returns URL to the avatar to display for the user
	return $userinfo['avatar'];
}

function getProfileURL($userinfo) {
	$url	= "http://www.livejournal.com/userinfo.bml?user=" . $userinfo['username'];
	return $url;
}

function checkAuth($username, $password, $modGroup, $checkAdmin) {
	// success: return user info array
	// failure: return error string
	global	$db;
	global	$dbprefix;
	global	$config;

	$cons	= array(
			'host'		=> 'www.livejournal.com',
			'path'		=> '/interface/xmlrpc',
			'port'		=> '80',
			'prefix'	=> 'LJ.XMLRPC.'
		);

	$args	= array(
			'username'	=> $username,
			'password'	=> $password,
			'getpickws'	=> 1,
			'getpickwurls'	=> 1,
			'getmoods'	=> 0
        	);

	$q	= new XML_RPC_QuickClient( $cons );
	$arr	= $q->login( $args );
	if ($arr->code) {
        	return "Login failed with error #{$arr->code}: {$arr->message}.";
	}

	$userinfo['username']	= $username;
	$userinfo['avatar']	= $arr['defaultpicurl'];
	$userinfo['user_id']	= $arr['userid'];
	$userinfo['moderator']	= 0;

	//
	// determine if they are a mod or not...
	//
	foreach ($arr['usejournals'] as $journal) {
		if ($journal == strtolower($config['LJ_MOD_COMMUNITY'])) {
			$userinfo['moderator']	= 1;
		}
	}

	return	$userinfo;
}

function checkBoardBan($ip, $userid) {
	// returns boolean result
	return	FALSE;
}

function checkValidAlias($alias, $uid) {
	// returns boolean result
	//
	// XX: Need to find a good way to check if an LJ user exists.
	//
	return	TRUE;
}

?>
